<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 9/7/12
 * Time: 4:05 PM
 * To change this template use File | Settings | File Templates.
 */
class Db_Locale_Untranslated extends Common {
    protected static $tableName = TAB_UNTRANSLATED;
    protected static $dbFields = array(
        'id',
        'ut_lang_id',
        'ut_module',
        'ut_key',
        'deleted'
    );

    public $objectArray;
    public $id;
    public $ut_lang_id;
    public $ut_module;
    public $ut_key;
    public $delete;

    function __construct($value = NULL, $fieldName = NULL) {
        parent::__construct($value, $fieldName);
    }

    /**
     * Are used for add new Untranslated
     * @static
     * @param $module
     * @param $key
     * @return bool
     */
    public static function setUntranslated($module, $key) {
        $languages = conf('LANGS');
        $sql = 'SELECT count(*) AS `cnt` FROM ?f WHERE `ut_module` = ? AND `ut_key` = ? AND `deleted` = 0 ;';

        $result = self::$DB->selectRow($sql, self::$tableName, $module, $key);
        if($result['cnt'] == 0) {
            foreach ($languages as $langId=>$langValue) {
                self::$DB->q("INSERT INTO ?f (`ut_lang_id`, `ut_module`, `ut_key`) VALUES (?, ?, ?)", self::$tableName, $langId, $module, $key);
            }
            if(self::$DB->affectedRows()){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Are used for delete untranslated after what is translated
     * @static
     * @param $langId
     * @param $module
     * @param $key
     * @return bool
     */
    public static function deleteUntranslated($langId, $module , $key) {
        self::$DB->q('DELETE FROM ?f WHERE `ut_lang_id` = ? AND `ut_module` = ? AND `ut_key` = ? ;', self::$tableName, $langId, $module, $key);
        return (self::$DB->affectedRows() ? true : false);
    }

    /**
     * Are used for delete all untranslated module and key
     * @static
     * @return bool
     */
    public static function deleteAllUntranslated() {
        return self::$DB->q('TRUNCATE TABLE ?f;', self::$tableName);
    }

}
